package com.anjubao.parking.municipal.parkinglot.task;


import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingStatRecordMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingStatRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 车位记录定时统计任务
 * @author anjubao
 */
//@Component
public class RecordTask {
    @Autowired
    private ParkingStatRecordMapper parkingStatRecordMapper;

    /**
     * 每隔一小时统计一次车场进出场数
     */
    @Scheduled(cron = "${task.cron}")
    public void refreshParkingStatRecord(){
        List<ParkingStatRecord> enterRecords = parkingStatRecordMapper.getParkingEnterRecords();
        List<ParkingStatRecord> leaveRecords = parkingStatRecordMapper.getParkingLeaveRecords();
        for(ParkingStatRecord enterParkingStatRecord:enterRecords){
            //离场数量先默认设为0
            enterParkingStatRecord.setLeaveRecords(0);
            enterParkingStatRecord.setId(U.id());
            enterParkingStatRecord.setCreateTime(LocalDateTime.now());
            //根据入场记录遍历出场记录看是否有匹配数据
            for(ParkingStatRecord leaveParkingStatRecord:leaveRecords){
                //入场记录与出场记录,时间以及车场id匹配
                if(enterParkingStatRecord.getStatTime().equals(leaveParkingStatRecord.getStatTime())&&
                        enterParkingStatRecord.getParkingLotId()==leaveParkingStatRecord.getParkingLotId()){
                    enterParkingStatRecord.setLeaveRecords(leaveParkingStatRecord.getLeaveRecords());
                   //符合即跳出循环，返回外层循环
                    break;
                }
            }
            parkingStatRecordMapper.insert(enterParkingStatRecord);
        }
    }

}
